package domain;

import java.util.Comparator;
import java.util.List;

import aima.core.agent.Action;
import aima.core.search.framework.EvaluationFunction;
import aima.core.search.framework.Metrics;
import aima.core.search.framework.Node;
import aima.core.search.framework.PrioritySearch;
import aima.core.search.framework.Problem;
import aima.core.search.framework.QueueSearch;
import aima.core.search.framework.Search;
import aima.core.util.datastructure.XYLocation;

public class DichsteBroodKruimelSearch extends PrioritySearch{

	public DichsteBroodKruimelSearch(QueueSearch search, EvaluationFunction ef) {
		super(search, createComparator(ef));
	}

	private static Comparator<Node> createComparator(final EvaluationFunction ef) {
		return new Comparator<Node>() {
			public int compare(Node n1, Node n2) {
				PacmanBoard board1 = (PacmanBoard) n1.getState();				
				PacmanAction action1=(PacmanAction)n1.getAction();
				XYLocation location1=action1.getLocation();
				XYLocation crumbClosestToLocation1=board1.getClosestCrumbTo(location1);
				
				PacmanBoard board2 = (PacmanBoard) n2.getState();				
				PacmanAction action2=(PacmanAction)n2.getAction();
				XYLocation location2=action2.getLocation();
				XYLocation crumbClosestToLocation2=board2.getClosestCrumbTo(location2);
				
				
				
				Double distance1 = Math.sqrt(Math.pow(location1.getXCoOrdinate() - crumbClosestToLocation1.getXCoOrdinate(),2) + Math.pow(location1.getYCoOrdinate() - crumbClosestToLocation1.getYCoOrdinate(), 2));
				Double distance2 = Math.sqrt(Math.pow(location2.getXCoOrdinate() - crumbClosestToLocation2.getXCoOrdinate(),2) + Math.pow(location2.getYCoOrdinate() - crumbClosestToLocation2.getYCoOrdinate(), 2));
				
				//Double f1 = ef.f(n1);
				//Double f2 = ef.f(n2);
				return distance1.compareTo(distance2);
			}
		};
	}

}
